<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * @file auth.php
 * @brief 通行证验证
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-02-20
 */
abstract class Auth extends Kohana_Auth { 

	//sid
	protected static $sid = null;

	public static function instance() 
	{
		if (isset($_GET['sid']))
			self::$sid = $_GET['sid'];
		parent::instance();
		self::$_instance->set_sid(self::$sid);
		return self::$_instance;
	}

	public function __construct( $config = array() )
	{
		$config['salt_pattern'] = preg_split('/,\s*/', Kohana::config('auth')->get('salt_pattern'));
		$this->_config = $config;
		
	}

	public function use_sid() {
		return !empty(self::$sid);
	}
	protected static function frombase64($str)
	{
		if(!$str) { return false; }
		$str = base64_decode($str);
		return $str;
	}
	protected static function tobase64($str)
	{
		if(!$str) { return false; }
		$str = base64_encode($str);
		$str = str_replace('=', '', $str);
		return $str;
	}

	public function set_sid($sid)
	{
		$sid = self::frombase64($sid);
		$this->_session = Session::instance('database', $sid);
	}

	public function get_sid()
	{
		if (isset($this->_session))
		{
			return self::tobase64($this->_session->id());
		}
	}

	public function ed_login($user, $rememberme = FALSE)
	{
		if(!$this->login($user['username'], $user['password'], $user['rememberme'])){ return false;}
		
		return true;
	}
}
